package com.gzhryc.shared_device.oem.code.member.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.member.dao.MemberVerificationVoucherDao;
import com.gzhryc.shared_device.oem.code.member.dao.db.Member;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberVerificationVoucher;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberVerificationVoucherEnum;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberVerificationVoucherSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class MemberVerificationVoucherService {

    static Logger log = Logger.getLogger(MemberVerificationVoucherService.class);

    public static MemberVerificationVoucherService self() {
        return MultiDBTools.getService(MemberVerificationVoucherService.class, true);
    }

    public static MemberVerificationVoucherService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, MemberVerificationVoucherService.class, true);
    }

    MemberVerificationVoucherDao dao;

    public MemberVerificationVoucherService(String jdbcKey) {
        dao = new MemberVerificationVoucherDao(jdbcKey);
    }


    public List<MemberVerificationVoucher> findBySearch(MemberVerificationVoucherSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        if(search.getAscState()){
            conditions.asc("state");
        }
        conditions.desc("createDate");
        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(MemberVerificationVoucherSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public MemberVerificationVoucher getById(String id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(MemberVerificationVoucher memberVerificationVoucher) throws LogicException {
        if(memberVerificationVoucher.getMemberId() == null){
            throw new LogicException("请填写会员ID");
        }
        if(StringTools.isBlank(memberVerificationVoucher.getMemberName())) {
            Member member = MemberService.self().getById(memberVerificationVoucher.getMemberId());
            if (member == null) {
                throw new LogicException("未找到会员信息");
            } else {
                memberVerificationVoucher.setMemberName(member.getNickname());
            }
        }
        memberVerificationVoucher.setCreateDate(new Date());
        try {
            return dao.insert(memberVerificationVoucher) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean use(MemberVerificationVoucher memberVerificationVoucher,Long userId,String userName){
        if(memberVerificationVoucher != null && userId != null && StringTools.isNotBlank(userName)) {
            Conditions conditions = new Conditions(MemberVerificationVoucher.class);
            conditions.and().is("id",memberVerificationVoucher.getId());
            conditions.and().is("state", MemberVerificationVoucherEnum.State.Normal.index());

            MemberVerificationVoucher updateEntity = new MemberVerificationVoucher();
            updateEntity.setState(MemberVerificationVoucherEnum.State.Used.index());
            updateEntity.setVerifiedUserId(userId);
            updateEntity.setVerifiedUserName(userName);
            updateEntity.setUseDate(new Date());

            try {
                return dao.updateNotNull(updateEntity,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean deleteById(String id) throws LogicException{
        try {
            return dao.deleteById(id) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }
}
